// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: "全部订单",
        isActive: true
      },
      {
        id: 1,
        value: "待付款",
        isActive: false
      },
      {
        id: 2,
        value: "待发货",
        isActive: false
      },
      {
        id: 3,
        value: "退款/退货",
        isActive: false
      }
    ],
    index: 0
  },
  startX: 0,
  endX: 0,
  startY: 0,
  endY: 0,
  tabsItemChange(e) {
    const {index} = e.detail;
    //修改源数据
    let {tabs} = this.data;
    tabs.forEach((v, i) => {
      i === index ? v.isActive = true : v.isActive = false;
    });
    this.setData({
      tabs,
      index
    });
  },
  onShow() {
    //onShow那不到页面栈options
    //获取小程序页面栈，长度最大为10
    // 拿取url
    const currentPages = getCurrentPages();
    let {type} = currentPages[currentPages.length - 1].options;
    //跳转到订单页时选择标题
    this.onTitle(type);
  },
  //修改页面标题
  onTitle(type) {
    let index = this.data.tabs.findIndex(v => v.id == type),
        tabs = this.data.tabs;
    tabs.forEach((v, i)  => v.isActive = i === index ? true : false);
   this.setData({
    tabs
   });
  },
  //滑动切换页面效果
  // 页面开始滑动
  onTouchStart(e) {
    this.startX = e.changedTouches[0].pageX;
  },
  //页面结束滑动
  onTouchEnd(e) {
    //拿取结束值
    this.endX = e.changedTouches[0].pageX;
    if ( Math.abs(this.startX - this.endX) > 160 ) {
      //拿取index赋值
      e.detail.index = (this.startX - this.endX > 0 ?this.data.index + 1 : this.data.index - 1);
      //如果到达最后一页
      e.detail.index = e.detail.index === this.data.tabs.length? 0 : e.detail.index;
      //如果到第一页
      e.detail.index = e.detail.index === -1? this.data.tabs.length - 1 : e.detail.index;
      // console.log(this.data.tabs);
      this.tabsItemChange(e);
    }  
  }
})